<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.blurry" clearable size="small" placeholder="模糊搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery" />
        <el-date-picker
          v-model="query.createTime"
          :default-time="['00:00:00','23:59:59']"
          type="daterange"
          range-separator=":"
          size="small"
          class="date-item"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
        <rrOperation :crud="crud" />
      </div>
      <crudOperation :permission="permission" />
    </div>
    <!--表单渲染-->
    <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="680px">
      <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="120px">
        <el-form-item label="行政区域名称" prop="county">
          <el-input v-model="form.county" placeholder="行政区域名称" style="width: 178px;" />
        </el-form-item>
        <el-form-item label="行政区域编码" prop="code">
          <el-input v-model="form.code" placeholder="行政区域编码" style="width: 178px;" />
        </el-form-item>
        <el-form-item label="上级类目" prop="pid" style="margin-top: 20px">
          <treeselect v-model="form.pid" :options="j_areas" style="width: 450px;" placeholder="选择上级类目" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" :tree-props="{children: 'children', hasChildren: 'hasChildren'}" row-key="id" @select="crud.selectChange" @select-all="crud.selectAllChange" @selection-change="crud.selectionChangeHandler">
      <el-table-column type="selection" width="55" />
      <el-table-column v-if="columns.visible('county')" :show-overflow-tooltip="true" label="区域名称" width="180px" prop="county" />
      <el-table-column v-if="columns.visible('pid')" prop="pid" align="center" label="父级">
        <template slot-scope="scope">
          {{ scope.row.pid }}
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('code')" :show-overflow-tooltip="true" width="120px" prop="code" label="行政区域编码" />
      <el-table-column v-permission="['admin','j_area:edit','j_area:del']" label="操作" width="130px" align="center" fixed="right">
        <template slot-scope="scope">
          <udOperation
            :data="scope.row"
            :permission="permission"
            msg="确定删除吗,如果存在下级节点则一并删除，此操作不能撤销！"
          />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  import crudJ_Area from '@/api/common/j_area'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  import CRUD, { presenter, header, form, crud } from '@crud/crud'
  import rrOperation from '@crud/RR.operation'
  import crudOperation from '@crud/CRUD.operation'
  import udOperation from '@crud/UD.operation'

  // crud交由presenter持有
  const defaultCrud = CRUD({ title: '行政区域名称', url: 'api/j_areas', crudMethod: { ...crudJ_Area }})
  const defaultForm = { id: null, county: null, pid: 0, code: null }
  export default {
    name: 'J_Area',
    components: { Treeselect, crudOperation, rrOperation, udOperation },
    mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
    data() {
      return {
        j_areas: [],
        permission: {
          add: ['admin', 'j_area:add'],
          edit: ['admin', 'j_area:edit'],
          del: ['admin', 'j_area:del']
        },
        rules: {
          county: [
            { required: true, message: '请输入行政区域名称', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '请输入行政区域编码', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 新增与编辑前做的操作
      [CRUD.HOOK.afterToCU](crud, form) {
        crudJ_Area.getJ_areasTree().then(res => {
          this.j_areas = []
          const j_area = { id: 0, label: '顶级类目', children: [] }
          j_area.children = res
          this.j_areas.push(j_area)
        })
      },
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
